﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;
using Common;
using System.Web.SessionState;
using System.Dynamic;
using Model;
using DAL;
using System.Data;
using System.Dynamic;
using System.Net;
using System.Text;
using System.Web.SessionState;
using JPushAPI;
using WxPayAPI;

namespace UI.tools
{
    /// <summary>
    /// API 的摘要说明
    /// </summary>
    public class API : IHttpHandler, IRequiresSessionState
    {
        static Dictionary<string, Func<int,int,string>> hs = new Dictionary<string, Func<int,int,string>>();
        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            string action = JobRequest.GetRequestString("act");
            string token = JobRequest.GetRequestString("token");
            string userid = JobRequest.GetRequestString("userid");
            Commons.Log(action);
            string result = string.Empty;
            if (Commons.CheckSign())
            {
                int adminid = 0;
                int userids = 0;
                if (!string.IsNullOrEmpty(token))
                {
                    adminid = DataConvert.ToInt(DESEncrypt.Decrypt(token));
                }
                if (!string.IsNullOrEmpty(userid))
                {
                    userids = DataConvert.ToInt(DESEncrypt.Decrypt(userid));
                }
                result = hs[action](adminid, userids);
            }
            else
            {
                result = Commons.GetFaild("身份验证失败");
            }
            context.Response.Write(result);
            context.Response.End();
        }
        static API()
        {
            /*首页*/
            #region 登录
            hs.Add("Login", (int adminid, int userid) =>
            {
                //参数
                string code = JobRequest.GetRequestString("code"); //code
                string realname = JobRequest.GetRequestString("realname"); //用户名
                string headimg = JobRequest.GetRequestString("headimg"); //头像
                int sex = JobRequest.GetRequestInt("sex"); //性别
                string province = JobRequest.GetRequestString("province"); //省
                string city = JobRequest.GetRequestString("city"); //市

                int source = JobRequest.GetRequestInt("source"); //来源 1.PC  2.Android  3.IOS
                //参数
                UserInfo info = new UserInfo();
                if (userid != 0)
                {
                    info = UserDAL.FindOneByID(userid);
                }
                else
                {
                    WeiXin wx = new WeiXin();
                    AdminInfo admin = AdminDAL.FindOneByID(adminid);
                    string url = string.Format("https://api.weixin.qq.com/sns/jscode2session?appid={0}&secret={1}&js_code={2}&grant_type=authorization_code", admin.AppId, admin.AppSecret, code);
                    string result = wx.HttpGet(url, "");
                    Commons.Log(result);
                    dynamic user = wx.ConvertJson(result);
                    UserInfo uu=UserDAL.FindByOpenID(user.openid);
                    if (uu.ID == 0)
                    {
                        UserInfo ui = new UserInfo()
                        {
                            OpenID = user.openid,
                            AdminID = adminid,
                            UserName = "",
                            UserPwd = "",
                            RealName = realname,
                            Mobile = "",
                            HeadImg = headimg,
                            Sex = Commons.Sex[sex],
                            Province = province,
                            City = city,
                            Area = "",
                            Score=0,
                            Grade=1,
                            Enable = 1,
                            SessionKey = user.session_key,
                            UserToken = "",
                            LoginCount = 0,
                            LastLoginDate = DateTime.Now,
                            CreateDate = DateTime.Now
                        };
                        int ID = UserDAL.Save(ui);
                        ui.ID = ID;
                        ui.UserID = DESEncrypt.Encrypt(ID.ToString());
                        info = ui;
                    }
                    else
                    {
                        CommonDAL.UpdateColumnByID(uu.ID, "OpenID", uu.OpenID, "[user]");
                        uu.UserID = DESEncrypt.Encrypt(uu.ID.ToString());
                        info = uu;
                    }

                }
                return Commons.GetSuccess("获取成功", info);
            });
            #endregion
            #region 获取首页数据
            hs.Add("Init_Index", (int adminid, int userid) =>
            {
                //参数
                int source = JobRequest.GetRequestInt("source"); //来源 1.PC  2.Android  3.IOS
                //参数
                List<AdInfo> adlist = AdDAL.FindTop(adminid, 100, 0);
                List<NewsInfo> newslist = new List<NewsInfo>();
                List<QuanInfo> quanlist = new List<QuanInfo>();
                List<ActiveInfo> activelist = new List<ActiveInfo>();
                List<TagsInfo> tagslist = TagsDAL.FindAll(adminid);
                List<CategoryInfo> categorylist = CategoryDAL.FindAll(adminid);
                List<ProductInfo> productlist = ProductDAL.FindAll(adminid);
                
                AdminInfo admin = AdminDAL.FindOneByID(adminid);
                List<dynamic> menulist = new List<dynamic>();
                List<dynamic> modulelist = new List<dynamic>();
                dynamic menu = null;
                dynamic module = null;
                string[] menus = null;
                string[] modules = null;
                //首页菜单
                menu = new ExpandoObject(); menus = admin.Menu1.Split('&'); menu.Sort = DataConvert.ToInt(menus[0]); menu.IsTop = menus[1]; menu.Name = menus[2]; menu.Icon = menus[3] == "/img/new.png" ? menus[3] : Commons.domain + menus[3]; menu.Path = menus[4]; if (menu.IsTop == "1") { menulist.Add(menu); }
                menu = new ExpandoObject(); menus = admin.Menu2.Split('&'); menu.Sort = DataConvert.ToInt(menus[0]); menu.IsTop = menus[1]; menu.Name = menus[2]; menu.Icon = menus[3] == "/img/hot.png" ? menus[3] : Commons.domain + menus[3]; menu.Path = menus[4]; if (menu.IsTop == "1") { menulist.Add(menu); }
                menu = new ExpandoObject(); menus = admin.Menu3.Split('&'); menu.Sort = DataConvert.ToInt(menus[0]); menu.IsTop = menus[1]; menu.Name = menus[2]; menu.Icon = menus[3] == "/img/cuxiao.png" ? menus[3] : Commons.domain + menus[3]; menu.Path = menus[4]; if (menu.IsTop == "1") { menulist.Add(menu); }
                menu = new ExpandoObject(); menus = admin.Menu4.Split('&'); menu.Sort = DataConvert.ToInt(menus[0]); menu.IsTop = menus[1]; menu.Name = menus[2]; menu.Icon = menus[3] == "/img/all.png" ? menus[3] : Commons.domain + menus[3]; menu.Path = menus[4]; if (menu.IsTop == "1") { menulist.Add(menu); }
                menu = new ExpandoObject(); menus = admin.Menu5.Split('&'); menu.Sort = DataConvert.ToInt(menus[0]); menu.IsTop = menus[1]; menu.Name = menus[2]; menu.Icon = Commons.domain + menus[3]; menu.Path = menus[4]; if (menu.IsTop == "1") { menulist.Add(menu); }
                menu = new ExpandoObject(); menus = admin.Menu6.Split('&'); menu.Sort = DataConvert.ToInt(menus[0]); menu.IsTop = menus[1]; menu.Name = menus[2]; menu.Icon = Commons.domain + menus[3]; menu.Path = menus[4]; if (menu.IsTop == "1") { menulist.Add(menu); }
                menu = new ExpandoObject(); menus = admin.Menu7.Split('&'); menu.Sort = DataConvert.ToInt(menus[0]); menu.IsTop = menus[1]; menu.Name = menus[2]; menu.Icon = Commons.domain + menus[3]; menu.Path = menus[4]; if (menu.IsTop == "1") { menulist.Add(menu); }
                menu = new ExpandoObject(); menus = admin.Menu8.Split('&'); menu.Sort = DataConvert.ToInt(menus[0]); menu.IsTop = menus[1]; menu.Name = menus[2]; menu.Icon = Commons.domain + menus[3]; menu.Path = menus[4]; if (menu.IsTop == "1") { menulist.Add(menu); }
                //首页模块
                module = new ExpandoObject(); modules = admin.News.Split('&'); module.Flag = "News"; module.Sort = DataConvert.ToInt(modules[0]); module.IsTop = modules[1]; module.Count = DataConvert.ToInt(modules[2]); module.Name = modules[3]; if (module.IsTop == "1") { modulelist.Add(module); newslist = NewsDAL.FindTop(adminid, 2, module.Count); }
                module = new ExpandoObject(); modules = admin.Quan.Split('&'); module.Flag = "Quan"; module.Sort = DataConvert.ToInt(modules[0]); module.IsTop = modules[1]; module.Count = DataConvert.ToInt(modules[2]); module.Name = modules[3]; if (module.IsTop == "1") { modulelist.Add(module); quanlist = QuanDAL.FindTop(adminid, module.Count); }
                module = new ExpandoObject(); modules = admin.Active.Split('&'); module.Flag = "Active"; module.Sort = DataConvert.ToInt(modules[0]); module.IsTop = modules[1]; module.Count = DataConvert.ToInt(modules[2]); module.Name = modules[3]; if (module.IsTop == "1") { modulelist.Add(module); activelist = ActiveDAL.FindTop(adminid, module.Count); }
                foreach (TagsInfo info in tagslist)
                {
                    //首页菜单
                    menu = new ExpandoObject();
                    menu.Sort = info.MenuSort;
                    menu.IsTop = info.MenuIsTop;
                    menu.Name = info.Name;
                    menu.Icon = Commons.domain + info.MenuIcon;
                    menu.Path = "/pages/product/product?tags="+info.ID;
                    if (menu.IsTop == 1) { menulist.Add(menu); }
                    //首页模块
                    module = new ExpandoObject();
                    module.Flag = "Product";
                    module.Sort = info.ModuleSort;
                    module.IsTop = info.ModuleIsTop;
                    module.Name = info.Name;
                    if (module.IsTop == 1) { module.ProductList = productlist.Where(x => x.Tags.Split(',').Contains(info.ID.ToString())).Take(info.Count).ToList(); modulelist.Add(module); }
                }
                foreach (CategoryInfo info in categorylist)
                {
                    //首页菜单
                    menu = new ExpandoObject();
                    menu.Sort = info.MenuSort;
                    menu.IsTop = info.MenuIsTop;
                    menu.Name = info.Name;
                    menu.Icon = Commons.domain + info.MenuIcon;
                    menu.Path = "/pages/product/product?category1id=" + info.ParentID + "&category2id="+info.ID;
                    if (menu.IsTop == 1) { menulist.Add(menu); }
                    //首页模块
                    module = new ExpandoObject();
                    module.Flag = "Product";
                    module.Sort = info.ModuleSort;
                    module.IsTop = info.ModuleIsTop;
                    module.Name = info.Name;
                    if (module.IsTop == 1) { module.ProductList = productlist.Where(x => x.Category1ID == info.ID || x.Category2ID == info.ID).Take(info.Count).ToList(); modulelist.Add(module); }
                }
                
                dynamic obj = new ExpandoObject();
                obj.admin = admin;
                obj.adlist1 = adlist.Where(x=>x.Position==1).ToList();
                obj.adlist2 = adlist.Where(x => x.Position == 2).ToList();
                obj.newslist = newslist;
                obj.quanlist = quanlist;
                obj.activelist = activelist;
                obj.menulist = menulist.OrderBy(x=>x.Sort).ToList();
                obj.modulelist = modulelist.OrderBy(x => x.Sort).ToList();
                return Commons.GetSuccess("获取成功", obj);
            });
            #endregion

            /*用户*/
            #region 获取1个用户
            hs.Add("Find_UserInfo", (int adminid, int userid) =>
            {
                //参数
                //参数
                UserInfo info = UserDAL.FindOneByID(userid);
                return Commons.GetSuccess("获取成功", info);
            });
            #endregion
            #region 修改用户资料
            hs.Add("Edit_User", (int adminid, int userid) =>
            {
                //参数
                string realname = JobRequest.GetRequestString("realname"); //真实姓名
                string headimg = JobRequest.GetRequestString("headimg"); //头像
                string sex = JobRequest.GetRequestString("sex"); //性别
                int source = JobRequest.GetRequestInt("source"); //来源 1.PC  2.Android  3.IOS
                //参数
                int ID = 0;
                UserInfo info = new UserInfo()
                {
                    ID = userid,
                    HeadImg = headimg,
                    RealName = realname,
                    Sex = sex,
                };
                ID = UserDAL.Edit(info);
                if (ID > 0)
                {
                    return Commons.GetSuccess("保存成功", ID);
                }
                else
                {
                    return Commons.GetFaild("保存成功");
                }
            });
            #endregion

            /*资讯*/
            #region 获取资讯
            hs.Add("Find_News", (int adminid, int userid) =>
            {
                //参数
                int page = JobRequest.GetRequestInt("page"); //第几页
                int newstypeid = JobRequest.GetRequestInt("newstypeid"); //资讯分类
                string keyword = JobRequest.GetRequestString("keyword"); //搜索关键词
                string begindate = JobRequest.GetRequestString("begindate"); //开始时间
                string enddate = JobRequest.GetRequestString("enddate"); //结束时间
                int source = JobRequest.GetRequestInt("source"); //来源 1.PC  2.Android  3.IOS
                //参数
                int count = 0;
                List<NewsInfo> list = NewsDAL.FindPage(newstypeid, keyword, adminid, begindate, enddate, page, Commons.PageSize, "", "", out count);
                return Commons.GetSuccess("获取成功", list, count);
            });
            #endregion
            #region 获取资讯详情
            hs.Add("Find_NewsInfo", (int adminid, int userid) =>
            {
                //参数
                int id = JobRequest.GetRequestInt("id"); //ID
                int source = JobRequest.GetRequestInt("source"); //来源 1.PC  2.Android  3.IOS
                //参数
                NewsInfo info = NewsDAL.FindOneByID(id);
                NewsDAL.IncClickCount(id);
                return Commons.GetSuccess("获取成功", info);
            });
            #endregion
            #region 获取资讯分类
            hs.Add("Find_NewsType", (int adminid, int userid) =>
            {
                //参数
                int source = JobRequest.GetRequestInt("source"); //来源 1.PC  2.Android  3.IOS
                //参数
                List<NewsTypeInfo> list = NewsTypeDAL.FindAll(adminid);
                return Commons.GetSuccess("获取成功", list, list.Count);
            });
            #endregion

            /*商户*/
            #region 获取商户信息
            hs.Add("Find_AdminInfo", (int adminid, int userid) =>
            {
                //参数
                int id = JobRequest.GetRequestInt("id"); //ID
                int source = JobRequest.GetRequestInt("source"); //来源 1.PC  2.Android  3.IOS
                //参数
                AdminInfo info = AdminDAL.FindOneByID(id);
                return Commons.GetSuccess("获取成功", info);
            });
            #endregion

            /*留言反馈*/
            #region 保存留言反馈
            hs.Add("Save_Message", (int adminid, int userid) =>
            {
                //参数
                int id = JobRequest.GetRequestInt("id"); //唯一ID
                string realname = JobRequest.GetRequestString("realname"); //真实姓名
                string mobile = JobRequest.GetRequestString("mobile"); //电话
                string context = JobRequest.GetRequestString("context"); //内容
                int source = JobRequest.GetRequestInt("source"); //来源 1.PC  2.Android  3.IOS
                //参数
                int ID = 0;
                MessageInfo info = new MessageInfo()
                {
                    ID = id,
                    AdminID = adminid,
                    RealName = realname,
                    Mobile = mobile,
                    Context = context,
                    CreateDate = DateTime.Now
                };
                if (id == 0)
                {
                    ID = MessageDAL.Save(info);
                }
                else
                {
                    ID = MessageDAL.Update(info);
                }
                if (ID > 0)
                {
                    return Commons.GetSuccess("保存成功", ID);
                }
                else
                {
                    return Commons.GetFaild("保存成功");
                }
            });
            #endregion


            /*产品*/
            #region 获取产品分类
            hs.Add("Find_Category", (int adminid, int userid) =>
            {
                //参数
                int source = JobRequest.GetRequestInt("source"); //来源 1.PC  2.Android  3.IOS
                //参数
                List<CategoryInfo> list = CategoryDAL.FindAll(adminid);
                dynamic obj = new ExpandoObject();
                obj.category1 = list.Where(x => x.ParentID == 0);
                obj.category2 = list.Where(x => x.ParentID != 0);
                if (list.Where(x => x.ParentID != 0).Count() == 0)
                {
                    CategoryInfo first = B.Instance<CategoryInfo>(list.Where(x => x.ParentID == 0).FirstOrDefault());
                    obj.category1id = first.ID;
                    obj.productlist = ProductDAL.FindByCategory1ID(adminid, first.ID);
                }
                return Commons.GetSuccess("获取成功", obj);
            });
            #endregion
            #region 获取产品
            hs.Add("Find_Product", (int adminid, int userid) =>
            {
                //参数
                int page = JobRequest.GetRequestInt("page"); //第几页
                int category1id = JobRequest.GetRequestInt("category1id"); //大分类ID
                int category2id = JobRequest.GetRequestInt("category2id"); //小分类ID
                string tags = JobRequest.GetRequestString("tags"); //标签
                string keyword = JobRequest.GetRequestString("keyword"); //搜索关键词
                string order = JobRequest.GetRequestString("order"); //排序字段
                string desc = JobRequest.GetRequestString("desc"); //升序降序
                string begindate = JobRequest.GetRequestString("begindate"); //开始时间
                string enddate = JobRequest.GetRequestString("enddate"); //结束时间
                int source = JobRequest.GetRequestInt("source"); //来源 1.PC  2.Android  3.IOS
                //参数
                int count = 0;
                List<ProductInfo> list = ProductDAL.FindPage(category1id, category2id, 1, tags, keyword, adminid, begindate, enddate, page, Commons.PageSize, order, desc, out count);
                return Commons.GetSuccess("获取成功", list, count);
            });
            #endregion
            #region 获取大分类下的产品
            hs.Add("Find_ProductByCategory1ID", (int adminid, int userid) =>
            {
                //参数
                int category1id = JobRequest.GetRequestInt("category1id"); //大分类ID
                int source = JobRequest.GetRequestInt("source"); //来源 1.PC  2.Android  3.IOS
                //参数
                dynamic obj = new ExpandoObject();
                obj.productlist = ProductDAL.FindByCategory1ID(adminid, category1id);
                return Commons.GetSuccess("获取成功", obj);
            });
            #endregion

            /*产品详情*/
            #region 获取产品详情
            hs.Add("Find_ProductInfo", (int adminid, int userid) =>
            {
                //参数
                int id = JobRequest.GetRequestInt("id"); //ID
                int source = JobRequest.GetRequestInt("source"); //来源 1.PC  2.Android  3.IOS
                //参数
                ProductInfo info = ProductDAL.FindOneByID(id);
                if (info.PriceType == 2)
                {
                    info.PriceList = PriceDAL.FindListByProductID(id);
                }
                List<ProductParamInfo> paramlist = ProductParamDAL.FindByProductID(adminid, id);
                List<ReplyInfo> replylist = ReplyDAL.FindTopByProductID(adminid, 3, id);
                info.ParamList = paramlist.Where(x => x.ParentID == 0).ToList();
                foreach (ProductParamInfo productparam in info.ParamList)
                {
                    productparam.ChildList = paramlist.Where(x => x.ParentID == productparam.ID).ToList();
                }
                List<dynamic> formatlist = new List<dynamic>();
                if (!string.IsNullOrEmpty(info.Format1))
                {
                    dynamic format = new ExpandoObject();
                    format.List = info.Format1.Split('&');
                    formatlist.Add(format);
                }
                if (!string.IsNullOrEmpty(info.Format2))
                {
                    dynamic format = new ExpandoObject();
                    format.List = info.Format2.Split('&');
                    formatlist.Add(format);
                }
                if (!string.IsNullOrEmpty(info.Format3))
                {
                    dynamic format = new ExpandoObject();
                    format.List = info.Format3.Split('&');
                    formatlist.Add(format);
                }
                if (!string.IsNullOrEmpty(info.Format4))
                {
                    dynamic format = new ExpandoObject();
                    format.List = info.Format4.Split('&');
                    formatlist.Add(format);
                }
                if (!string.IsNullOrEmpty(info.Format5))
                {
                    dynamic format = new ExpandoObject();
                    format.List = info.Format5.Split('&');
                    formatlist.Add(format);
                }
                info.FormatList = formatlist;
                info.IsStore = StoreDAL.IsStore(adminid, id, userid);
                info.ReplyList = replylist;
                info.ReplyCount = ReplyDAL.FindCountByProductID(adminid, id);
                ProductDAL.IncClickCount(id);
                HistoryDAL.Save(new HistoryInfo() { AdminID=adminid, ProductID=id, UserID=userid, CreateDate=DateTime.Now });
                return Commons.GetSuccess("获取成功", info);
            });
            #endregion

            /*购物车*/
            #region 保存购物车
            hs.Add("Save_Car", (int adminid, int userid) =>
            {
                //参数
                int id = JobRequest.GetRequestInt("id"); //唯一ID
                int productid = JobRequest.GetRequestInt("productid"); //产品ID
                float oprice = JobRequest.GetRequestFloat("oprice"); //原价
                float aprice = JobRequest.GetRequestFloat("aprice"); //现价
                float pprice = JobRequest.GetRequestFloat("pprice"); //拼团价格
                string unit = JobRequest.GetRequestString("unit"); //单位
                int count = JobRequest.GetRequestInt("count"); //购买数量
                int ischeck = JobRequest.GetRequestInt("ischeck"); //是否选中（0.未选中  1.已选中）
                string format = JobRequest.GetRequestString("format"); //规格
                string flag = JobRequest.GetRequestString("flag"); //标记 add:加入购物车  update:修改购物车数量
                int source = JobRequest.GetRequestInt("source"); //来源 1.PC  2.Android  3.IOS
                //参数
                CarInfo car = new CarInfo();
                int ID = 0;
                CarInfo info = new CarInfo()
                {
                    ID = id,
                    AdminID = adminid,
                    UserID = userid,
                    ProductID = productid,
                    OPrice = oprice,
                    APrice = aprice,
                    PPrice = pprice,
                    Unit = unit,
                    Count = count,
                    IsCheck = ischeck,
                    Format = format,
                    CreateDate = DateTime.Now
                };
                car = CarDAL.FindIDByUserIDAndProductID(adminid, userid, productid, format);
                if (car.ID == 0)
                {
                    ID = CarDAL.Save(info);
                }
                else
                {
                    if (flag == "add")
                    {
                        count += car.Count;
                    }
                    ID = CarDAL.Update(car.ID,count);
                }
                if (ID > 0)
                {
                    return Commons.GetSuccess("保存成功", ID);
                }
                else
                {
                    return Commons.GetFaild("保存成功");
                }
            });
            #endregion
            #region 获取购物车
            hs.Add("Find_Car", (int adminid, int userid) =>
            {
                //参数
                int source = JobRequest.GetRequestInt("source"); //来源 1.PC  2.Android  3.IOS
                //参数
                int count = 0;
                List<CarInfo> list = CarDAL.FindListByUserID(adminid,userid);
                return Commons.GetSuccess("获取成功", list, count);
            });
            #endregion
            #region 删除购物车
            hs.Add("Delete_Car", (int adminid, int userid) =>
            {
                //参数
                string ids = JobRequest.GetRequestString("ids"); //删除的id集合,中间用,号隔开
                int source = JobRequest.GetRequestInt("source"); //来源 1.PC  2.Android  3.IOS
                //参数
                bool ok = CommonDAL.DeleteByIDs(ids, "[car]");
                if (ok)
                {
                    return Commons.GetSuccess("删除成功", "");
                }
                else
                {
                    return Commons.GetFaild("删除失败");
                }
            });
            #endregion
            #region 设置是否选中
            hs.Add("Set_IsCheck", (int adminid, int userid) =>
            {
                //参数
                string ids = JobRequest.GetRequestString("ids"); //删除的id集合,中间用,号隔开
                int source = JobRequest.GetRequestInt("source"); //来源 1.PC  2.Android  3.IOS
                //参数
                bool ok = CarDAL.Set_IsCheck(adminid, userid, ids);
                if (ok)
                {
                    return Commons.GetSuccess("设置成功", "");
                }
                else
                {
                    return Commons.GetFaild("设置失败");
                }
            });
            #endregion

            /*收货地址*/
            #region 获取收货地址
            hs.Add("Find_Address", (int adminid, int userid) =>
            {
                //参数
                int source = JobRequest.GetRequestInt("source"); //来源 1.PC  2.Android  3.IOS
                //参数
                int count = 0;
                List<AddressInfo> list = AddressDAL.FindByUserID(adminid, userid);
                return Commons.GetSuccess("获取成功", list, count);
            });
            #endregion
            #region 获取收货地址详情
            hs.Add("Find_AddressInfo", (int adminid, int userid) =>
            {
                //参数
                int id = JobRequest.GetRequestInt("id"); //ID
                int source = JobRequest.GetRequestInt("source"); //来源 1.PC  2.Android  3.IOS
                //参数
                AddressInfo info = AddressDAL.FindOneByID(id);
                return Commons.GetSuccess("获取成功", info);
            });
            #endregion
            #region 获取用户默认收货地址
            hs.Add("Find_DefaultAddress", (int adminid, int userid) =>
            {
                //参数
                double man = JobRequest.GetRequestDouble("man"); //满减额度
                int source = JobRequest.GetRequestInt("source"); //来源 1.PC  2.Android  3.IOS
                //参数
                AddressInfo address = AddressDAL.FindByDefault(adminid, userid);
                MyQuanInfo quan = MyQuanDAL.FindMaxQuan(adminid, userid, man);
                dynamic obj = new ExpandoObject();
                obj.address = address;
                obj.quan = quan;
                return Commons.GetSuccess("获取成功", obj);
            });
            #endregion
            #region 保存收货地址
            hs.Add("Save_Address", (int adminid, int userid) =>
            {
                //参数
                int id = JobRequest.GetRequestInt("id"); //
                string province = JobRequest.GetRequestString("province"); //
                string city = JobRequest.GetRequestString("city"); //
                string area = JobRequest.GetRequestString("area"); //
                string realname = JobRequest.GetRequestString("realname"); //真实姓名
                string mobile = JobRequest.GetRequestString("mobile"); //手机号码
                string address = JobRequest.GetRequestString("address"); //详细地址
                int source = JobRequest.GetRequestInt("source"); //来源 1.PC  2.Android  3.IOS
                //参数
                if (AddressDAL.IsExist(id, userid, realname, mobile, address))
                {
                    return Commons.GetFaild("该地址已存在");
                }
                if (province == "undefined" || realname == "undefined")
                {
                    return Commons.GetFaild("地址有误，请重新操作");
                }
                int ID = 0;
                AddressInfo info = new AddressInfo()
                {
                    ID = id,
                    AdminID = adminid,
                    UserID = userid,
                    Province = province,
                    City = city,
                    Area = area,
                    RealName = realname,
                    Mobile = mobile,
                    Address = address,
                    IsDefault = 1,
                    CreateDate = DateTime.Now
                };
                if (id == 0)
                {
                    ID = AddressDAL.Save(info);
                }
                else
                {
                    ID = AddressDAL.Update(info);
                }
                if (ID > 0)
                {
                    return Commons.GetSuccess("保存成功", ID);
                }
                else
                {
                    return Commons.GetFaild("保存失败");
                }
            });
            #endregion
            #region 删除收货地址
            hs.Add("Delete_Address", (int adminid, int userid) =>
            {
                //参数
                string ids = JobRequest.GetRequestString("ids"); //删除的id集合,中间用,号隔开
                int source = JobRequest.GetRequestInt("source"); //来源 1.PC  2.Android  3.IOS
                //参数
                bool ok = CommonDAL.DeleteByIDs(ids, "[address]");
                if (ok)
                {
                    return Commons.GetSuccess("删除成功", "");
                }
                else
                {
                    return Commons.GetFaild("删除失败");
                }
            });
            #endregion
            #region 设置是否默认
            hs.Add("Set_IsDefault", (int adminid, int userid) =>
            {
                //参数
                int id = JobRequest.GetRequestInt("id"); //地址id
                int source = JobRequest.GetRequestInt("source"); //来源 1.PC  2.Android  3.IOS
                //参数
                bool ok = AddressDAL.Set_IsDefault(adminid, userid, id);
                if (ok)
                {
                    return Commons.GetSuccess("设置成功", "");
                }
                else
                {
                    return Commons.GetFaild("设置失败");
                }
            });
            #endregion

            /*订单*/
            #region 获取订单
            hs.Add("Find_POrder", (int adminid, int userid) =>
            {
                //参数
                int page = JobRequest.GetRequestInt("page"); //第几页
                int state = JobRequest.GetRequestInt("state"); //订单状态
                string begindate = JobRequest.GetRequestString("begindate"); //开始时间
                string enddate = JobRequest.GetRequestString("enddate"); //结束时间
                int source = JobRequest.GetRequestInt("source"); //来源 1.PC  2.Android  3.IOS
                //参数
                int count = 0;
                List<POrderInfo> list = POrderDAL.FindPage(adminid, userid, state, begindate, enddate, page, Commons.PageSize, "", "", out count);
                if (list.Count > 0)
                {
                    List<POrderDetailInfo> childlist = POrderDetailDAL.FindListByOrderIDs(adminid, string.Join(",", list.Select(x => x.ID)));
                    foreach (POrderInfo info in list)
                    {
                        info.ChildList = childlist.Where(x => x.OrderID == info.ID).ToList();
                    }
                }
                return Commons.GetSuccess("获取成功", list, count);
            });
            #endregion
            #region 获取订单与订单详细
            hs.Add("Find_POrderByID", (int adminid, int userid) =>
            {
                //参数
                int orderid = JobRequest.GetRequestInt("orderid"); //订单ID
                int source = JobRequest.GetRequestInt("source"); //来源 1.PC  2.Android  3.IOS
                //参数
                List<POrderDetailInfo> list = POrderDetailDAL.FindListByOrderID(adminid, userid, orderid);
                POrderInfo info = POrderDAL.FindOneByID(orderid);
                string post = "";
                if (info.State >= 2)
                {
                    post = KuaiDi.Find_Express(Commons.PostCom[info.Logistics], info.ExpressNum);
                }
                WeiXin wx=new WeiXin();
                info.ChildList = list;
                info.Post = post;
                if (post != "")
                {
                    dynamic obj = wx.ConvertJson(post);
                    info.PostState = Commons.PostState[DataConvert.ToInt(obj.State)];
                }
                return Commons.GetSuccess("获取成功", info);
            });
            #endregion
            #region 获取订单详细
            hs.Add("Find_POrderDetail", (int adminid, int userid) =>
            {
                //参数
                int orderid = JobRequest.GetRequestInt("orderid"); //订单ID
                int source = JobRequest.GetRequestInt("source"); //来源 1.PC  2.Android  3.IOS
                //参数
                int count = 0;
                List<POrderDetailInfo> list = POrderDetailDAL.FindListByOrderID(adminid, userid, orderid);
                return Commons.GetSuccess("获取成功", list, count);
            });
            #endregion
            #region 确认收货
            hs.Add("Sure_POrder", (int adminid, int userid) =>
            {
                //参数
                int id = JobRequest.GetRequestInt("id"); //订单ID
                int source = JobRequest.GetRequestInt("source"); //来源 1.PC  2.Android  3.IOS
                //参数
                bool ok = POrderDAL.UpdateState(3, adminid, userid, id);
                if (ok)
                {
                    return Commons.GetSuccess("收货成功", "");
                }
                else
                {
                    return Commons.GetFaild("收货失败");
                }
            });
            #endregion
            #region 删除订单
            hs.Add("Delete_POrder", (int adminid, int userid) =>
            {
                //参数
                string ids = JobRequest.GetRequestString("ids"); //删除的id集合,中间用,号隔开
                int source = JobRequest.GetRequestInt("source"); //来源 1.PC  2.Android  3.IOS
                //参数
                bool ok = CommonDAL.DeleteByIDs(ids, "[porder]");
                if (ok)
                {
                    return Commons.GetSuccess("删除成功", "");
                }
                else
                {
                    return Commons.GetFaild("删除失败");
                }
            });
            #endregion
            #region 获取订单数量
            hs.Add("Find_StateCount", (int adminid, int userid) =>
            {
                //参数
                int source = JobRequest.GetRequestInt("source"); //来源 1.PC  2.Android  3.IOS
                //参数
                List<dynamic> list = POrderDAL.FindStateCount(adminid, userid);
                dynamic obj = new ExpandoObject();
                obj.State0 = list.Where(x => x.State == 0).Count() > 0 ? list.FirstOrDefault(x => x.State == 0).Count : 0;
                obj.State1 = list.Where(x => x.State == 1).Count() > 0 ? list.FirstOrDefault(x => x.State == 1).Count : 0;
                obj.State2 = list.Where(x => x.State == 2).Count() > 0 ? list.FirstOrDefault(x => x.State == 2).Count : 0;
                obj.State3 = list.Where(x => x.State == 3).Count() > 0 ? list.FirstOrDefault(x => x.State == 3).Count : 0;
                obj.State5 = list.Where(x => x.State == 5).Count() > 0 ? list.FirstOrDefault(x => x.State == 5).Count : 0;
                return Commons.GetSuccess("获取成功", obj);
            });
            #endregion
            #region 获取快递物流信息
            hs.Add("Find_Express", (int adminid, int userid) =>
            {
                //参数
                string expressnum = JobRequest.GetRequestString("expressnum");
                string logistics = JobRequest.GetRequestString("logistics");
                int source = JobRequest.GetRequestInt("source"); //来源 1.PC  2.Android  3.IOS
                //参数
                string html = KuaiDi.Find_Express(logistics, expressnum);
                return html;
            });
            #endregion
            #region 提醒发货
            hs.Add("Remind_POrder", (int adminid, int userid) =>
            {
                //参数
                int id = JobRequest.GetRequestInt("id"); //订单ID
                int source = JobRequest.GetRequestInt("source"); //来源 1.PC  2.Android  3.IOS
                //参数
                bool ok = POrderDAL.RemindPOrder(adminid, userid, id);
                if (ok)
                {
                    return Commons.GetSuccess("提醒成功", "");
                }
                else
                {
                    return Commons.GetFaild("提醒失败");
                }
            });
            #endregion


            /*退货换货*/
            #region 获取退货换货
            hs.Add("Find_Tui", (int adminid, int userid) =>
            {
                //参数
                int page = JobRequest.GetRequestInt("page"); //第几页
                int limit = JobRequest.GetRequestInt("limit"); //每页条数
                string keyword = JobRequest.GetRequestString("keyword"); //搜索关键词
                string begindate = JobRequest.GetRequestString("begindate"); //开始时间
                string enddate = JobRequest.GetRequestString("enddate"); //结束时间
                int source = JobRequest.GetRequestInt("source"); //来源 1.PC  2.Android  3.IOS
                //参数
                int count = 0;
                List<TuiInfo> list = TuiDAL.FindPage(keyword, begindate, enddate, page, limit, "", "", out count);
                return Commons.GetSuccess("获取成功", list, count);
            });
            #endregion
            #region 保存退货换货
            hs.Add("Save_Tui", (int adminid, int userid) =>
            {
                //参数
                int id = JobRequest.GetRequestInt("id"); //唯一ID
                int orderid = JobRequest.GetRequestInt("orderid"); //订单ID
                int orderdetailid = JobRequest.GetRequestInt("orderdetailid"); //
                int productid = JobRequest.GetRequestInt("productid"); //产品ID
                string type = JobRequest.GetRequestString("type"); //申请类型
                string reason = JobRequest.GetRequestString("reason"); //申请原因
                float money = JobRequest.GetRequestFloat("money"); //退款金额
                string mobile = JobRequest.GetRequestString("mobile"); //联系电话
                string context = JobRequest.GetRequestString("context"); //回复内容
                int source = JobRequest.GetRequestInt("source"); //来源 1.PC  2.Android  3.IOS
                //参数
                int ID = 0;
                TuiInfo info = new TuiInfo()
                {
                    ID = id,
                    AdminID = adminid,
                    UserID = userid,
                    OrderID = orderid,
                    OrderDetailID = orderdetailid,
                    ProductID = productid,
                    Type = type,
                    Reason = reason,
                    Money = money,
                    Mobile = mobile,
                    Context = context,
                    CreateDate = DateTime.Now
                };
                if (id == 0)
                {
                    ID = TuiDAL.Save(info);
                }
                else
                {
                    ID = TuiDAL.Update(info);
                }
                if (ID > 0)
                {
                    POrderDAL.UpdateState(5, adminid, userid, orderid);
                    return Commons.GetSuccess("保存成功", ID);
                }
                else
                {
                    return Commons.GetFaild("保存成功");
                }
            });
            #endregion
            #region 删除退货换货
            hs.Add("Delete_Tui", (int adminid, int userid) =>
            {
                //参数
                string ids = JobRequest.GetRequestString("ids"); //删除的id集合,中间用,号隔开
                int source = JobRequest.GetRequestInt("source"); //来源 1.PC  2.Android  3.IOS
                //参数
                bool ok = CommonDAL.DeleteByIDs(ids, "[tui]");
                if (ok)
                {
                    return Commons.GetSuccess("删除成功", "");
                }
                else
                {
                    return Commons.GetFaild("删除失败");
                }
            });
            #endregion


            /*获取结算商品*/
            #region 获取结算商品
            hs.Add("Find_Pay", (int adminid, int userid) =>
            {
                //参数
                int source = JobRequest.GetRequestInt("source"); //来源 1.PC  2.Android  3.IOS
                //参数
                int count = 0;
                List<CarInfo> carlist = CarDAL.FindListByPay(adminid, userid);
                AddressInfo address = AddressDAL.FindByDefault(adminid, userid);
                MyQuanInfo quan = MyQuanDAL.FindMaxQuan(adminid, userid, carlist.Sum(x => x.APrice * x.Count));
                dynamic obj = new ExpandoObject();
                obj.carlist = carlist;
                obj.address = address;
                obj.quan = quan;
                return Commons.GetSuccess("获取成功", obj, count);
            });
            #endregion

            /*回复*/
            #region 获取回复
            hs.Add("Find_Reply", (int adminid, int userid) =>
            {
                //参数
                int page = JobRequest.GetRequestInt("page"); //第几页
                int productid = JobRequest.GetRequestInt("productid"); //商品ID
                string keyword = JobRequest.GetRequestString("keyword"); //搜索关键词
                string begindate = JobRequest.GetRequestString("begindate"); //开始时间
                string enddate = JobRequest.GetRequestString("enddate"); //结束时间
                int source = JobRequest.GetRequestInt("source"); //来源 1.PC  2.Android  3.IOS
                //参数
                int count = 0;
                List<ReplyInfo> list = ReplyDAL.FindPage(productid, keyword, begindate, enddate, page, Commons.PageSize, "", "", out count);
                return Commons.GetSuccess("获取成功", list, count);
            });
            #endregion
            #region 保存回复
            hs.Add("Save_Reply", (int adminid, int userid) =>
            {
                //参数
                int id = JobRequest.GetRequestInt("id"); //唯一ID
                int orderid = JobRequest.GetRequestInt("orderid"); //订单ID
                int introscore = JobRequest.GetRequestInt("introscore"); //描述相符
                int serverscore = JobRequest.GetRequestInt("serverscore"); //服务态度
                int postscore = JobRequest.GetRequestInt("postscore"); //物流服务
                string replydetail = JobRequest.GetRequestString("replydetail"); //回复内容
                int source = JobRequest.GetRequestInt("source"); //来源 1.PC  2.Android  3.IOS
                //参数
                int ID = 0;
                string[] replydetails = replydetail.Split('|');
                List<ReplyInfo> list = new List<ReplyInfo>();
                foreach (string detail in replydetails)
                {
                    string[] details = detail.Split('&');
                    ReplyInfo info = new ReplyInfo()
                    {
                        AdminID = adminid,
                        UserID = userid,
                        OrderID = orderid,
                        OrderDetailID = DataConvert.ToInt(details[0]),
                        ProductID = DataConvert.ToInt(details[1]),
                        IntroScore = introscore,
                        ServerScore = serverscore,
                        PostScore = postscore,
                        Format = details[2],
                        Context = details[3],
                        CreateDate = DateTime.Now
                    };
                    ID = ReplyDAL.Save(info);
                    info.ID = ID;
                    list.Add(info);
                }
                if (ID > 0)
                {
                    POrderDAL.UpdateState(4,adminid,userid,orderid);
                    return Commons.GetSuccess("保存成功", list);
                }
                else
                {
                    return Commons.GetFaild("保存成功");
                }
            });
            #endregion
            #region 删除回复
            hs.Add("Delete_Reply", (int adminid, int userid) =>
            {
                //参数
                string ids = JobRequest.GetRequestString("ids"); //删除的id集合,中间用,号隔开
                int source = JobRequest.GetRequestInt("source"); //来源 1.PC  2.Android  3.IOS
                //参数
                bool ok = CommonDAL.DeleteByIDs(ids, "[reply]");
                if (ok)
                {
                    return Commons.GetSuccess("删除成功", "");
                }
                else
                {
                    return Commons.GetFaild("删除失败");
                }
            });
            #endregion

            /*收藏*/
            #region 获取收藏
            hs.Add("Find_Store", (int adminid, int userid) =>
            {
                //参数
                int page = JobRequest.GetRequestInt("page"); //第几页
                string keyword = JobRequest.GetRequestString("keyword"); //搜索关键词
                string begindate = JobRequest.GetRequestString("begindate"); //开始时间
                string enddate = JobRequest.GetRequestString("enddate"); //结束时间
                int source = JobRequest.GetRequestInt("source"); //来源 1.PC  2.Android  3.IOS
                //参数
                int count = 0;
                List<StoreInfo> list = StoreDAL.FindPage(userid, keyword, begindate, enddate, page, Commons.PageSize, "", "", out count);
                return Commons.GetSuccess("获取成功", list, count);
            });
            #endregion
            #region 保存收藏
            hs.Add("Save_Store", (int adminid, int userid) =>
            {
                //参数
                int productid = JobRequest.GetRequestInt("productid"); //商品ID
                string flag = JobRequest.GetRequestString("flag"); //标记 store:收藏  cancel:取消
                int source = JobRequest.GetRequestInt("source"); //来源 1.PC  2.Android  3.IOS
                //参数
                int ID = 0;
                StoreInfo info = new StoreInfo()
                {
                    AdminID = adminid,
                    ProductID = productid,
                    UserID = userid,
                    CreateDate = DateTime.Now
                };
                if (flag=="store")
                {
                    ID = StoreDAL.Save(info);
                }
                else
                {
                    ID = StoreDAL.Delete(info);
                }
                if (ID > 0)
                {
                    return Commons.GetSuccess((flag=="store"?"收藏":"取消")+"成功", ID);
                }
                else
                {
                    return Commons.GetFaild("收藏失败");
                }
            });
            #endregion

            /*足迹*/
            #region 获取足迹
            hs.Add("Find_History", (int adminid, int userid) =>
            {
                //参数
                int page = JobRequest.GetRequestInt("page"); //第几页
                string keyword = JobRequest.GetRequestString("keyword"); //搜索关键词
                string begindate = JobRequest.GetRequestString("begindate"); //开始时间
                string enddate = JobRequest.GetRequestString("enddate"); //结束时间
                int source = JobRequest.GetRequestInt("source"); //来源 1.PC  2.Android  3.IOS
                //参数
                int count = 0;
                List<HistoryInfo> list = HistoryDAL.FindPage(userid, keyword, begindate, enddate, page, Commons.PageSize, "", "", out count);
                return Commons.GetSuccess("获取成功", list, count);
            });
            #endregion

            /*积分*/
            #region 获取积分
            hs.Add("Find_Score", (int adminid, int userid) =>
            {
                //参数
                int page = JobRequest.GetRequestInt("page"); //第几页
                int type = JobRequest.GetRequestInt("type"); //类型  1.收入  2.支出
                int source = JobRequest.GetRequestInt("source"); //来源 1.PC  2.Android  3.IOS
                //参数
                int count = 0;
                List<ScoreInfo> list = ScoreDAL.FindPage(userid, type, page, Commons.PageSize, "", "", out count);
                return Commons.GetSuccess("获取成功", list, count);
            });
            #endregion

            /*我的优惠券*/
            #region 获取我的优惠券
            hs.Add("Find_MyQuan", (int adminid, int userid) =>
            {
                //参数
                int page = JobRequest.GetRequestInt("page"); //第几页
                int state = JobRequest.GetRequestInt("state"); //状态
                int source = JobRequest.GetRequestInt("source"); //来源 1.PC  2.Android  3.IOS
                //参数
                int count = 0;
                List<MyQuanInfo> list = MyQuanDAL.FindPage(userid, state, page, Commons.PageSize, "Money", "asc", out count);
                return Commons.GetSuccess("获取成功", list, count);
            });
            #endregion
            #region 获取我可用的优惠券
            hs.Add("Find_CanUseQuan", (int adminid, int userid) =>
            {
                //参数
                double man=JobRequest.GetRequestDouble("man"); //满减金额
                int source = JobRequest.GetRequestInt("source"); //来源 1.PC  2.Android  3.IOS
                //参数
                List<MyQuanInfo> list = MyQuanDAL.FindCanUseQuan(adminid, userid, man);
                return Commons.GetSuccess("获取成功", list);
            });
            #endregion
            #region 保存我的优惠券
            hs.Add("Save_MyQuan", (int adminid, int userid) =>
            {
                //参数
                int quanid = JobRequest.GetRequestInt("quanid"); //优惠券ID
                int source = JobRequest.GetRequestInt("source"); //来源 1.PC  2.Android  3.IOS
                //参数
                int ID = 0;
                if (MyQuanDAL.IsExist(adminid, userid, quanid))
                {
                    return Commons.GetFaild("您已领过该券");
                }
                QuanInfo quan = QuanDAL.FindOneByID(quanid);
                MyQuanInfo info = new MyQuanInfo()
                {
                    AdminID = adminid,
                    UserID = userid,
                    QuanID = quanid,
                    Money = quan.Money,
                    Man = quan.Man,
                    State = 0,
                    BeginDate = quan.BeginDate,
                    EndDate = quan.EndDate,
                    CreateDate = DateTime.Now
                };
                ID = MyQuanDAL.Save(info);
                if (ID > 0)
                {
                    return Commons.GetSuccess("领取成功", ID);
                }
                else
                {
                    return Commons.GetFaild("领取失败");
                }
            });
            #endregion

            /*广告*/
            #region 获取广告详情
            hs.Add("Find_AdInfo", (int adminid, int userid) =>
            {
                //参数
                int id = JobRequest.GetRequestInt("id"); //ID
                int source = JobRequest.GetRequestInt("source"); //来源 1.PC  2.Android  3.IOS
                //参数
                AdInfo info = AdDAL.FindOneByID(id);
                AdDAL.IncClickCount(id);
                return Commons.GetSuccess("获取成功", info);
            });
            #endregion



            /*热门关键词*/
            #region 获取热门关键词
            hs.Add("Find_Hot", (int adminid, int userid) =>
            {
                //参数
                int source = JobRequest.GetRequestInt("source"); //来源 1.PC  2.Android  3.IOS
                //参数
                string[] hot = CommonDAL.FindColumnByID(adminid, "Hot", "admin").Replace("，",",").Split(',');
                return Commons.GetSuccess("获取成功", hot);
            });
            #endregion


            #region 获取OpenID
            hs.Add("Find_OpenID", (int adminid, int userid) =>
            {
                //参数
                string code = JobRequest.GetRequestString("code"); 
                int source = JobRequest.GetRequestInt("source"); //来源 1.PC  2.Android  3.IOS
                //参数
                WeiXin wx = new WeiXin();
                string url=string.Format("https://api.weixin.qq.com/sns/jscode2session?appid={0}&secret={1}&js_code={2}&grant_type=authorization_code", WxPayConfig.APPID, WxPayConfig.APPSECRET,code);
                string result = wx.HttpGet(url,"");
                dynamic user = wx.ConvertJson(result);

                return Commons.GetSuccess("获取成功", result);
            });
            #endregion
            #region 获取微信支付参数
            hs.Add("GetWxParam1", (int adminid, int userid) =>
            {
                //参数
                int quanid = JobRequest.GetRequestInt("quanid"); //优惠券ID
                string openid = JobRequest.GetRequestString("openid"); //开放ID
                double money = JobRequest.GetRequestDouble("money"); //商品金额
                double postmoney = JobRequest.GetRequestDouble("postmoney"); //快递金额
                double allmoney = JobRequest.GetRequestDouble("allmoney"); //实付金额
                int score = JobRequest.GetRequestInt("score"); //积分
                int count = JobRequest.GetRequestInt("count"); //商品数量
                double scoremoney = JobRequest.GetRequestDouble("scoremoney"); //积分抵扣
                double quanmoney = JobRequest.GetRequestDouble("quanmoney"); //优惠券抵扣
                int productid = JobRequest.GetRequestInt("productid"); //产品ID
                string province = JobRequest.GetRequestString("province"); //省
                string city = JobRequest.GetRequestString("city"); //市
                string area = JobRequest.GetRequestString("area"); //区
                string realname = JobRequest.GetRequestString("realname"); //收货人姓名
                string mobile = JobRequest.GetRequestString("mobile"); //联系电话
                string address = JobRequest.GetRequestString("address"); //收货地址
                string format = JobRequest.GetRequestString("format"); //收货地址
                //参数
                if (string.IsNullOrEmpty(openid) || openid=="undefined")
                {
                    openid = UserDAL.FindOneByID(userid).OpenID;
                }
                if (province == "undefined" || realname == "undefined")
                {
                    return Commons.GetFaild("地址有误，请重新操作");
                }
                POrderInfo info = new POrderInfo()
                {
                    AdminID = adminid,
                    UserID = userid,
                    QuanID = quanid,
                    Num = Commons.ToNum("PO", CommonDAL.GetNum(adminid, DateHelper.CurrentDay.StartDate, DateHelper.CurrentDay.EndDate, "POrder")),
                    Money = money,
                    PostMoney = postmoney,
                    ScoreMoney = scoremoney,
                    QuanMoney = quanmoney,
                    AllMoney = allmoney,
                    Score = score,
                    Count = count,
                    Logistics = "",
                    ExpressNum = "",
                    Province = province,
                    City = city,
                    Area = area,
                    RealName = realname,
                    Mobile = mobile,
                    Address = address,
                    State = 0,
                    CreateDate = DateTime.Now
                };
                int id = POrderDAL.Save(info);
                if (id > 0)
                {
                    if (productid == 0)
                    {
                        List<CarInfo> carlist = CarDAL.FindListByPay(adminid, userid);
                        foreach (CarInfo car in carlist)
                        {
                            POrderDetailInfo detail = new POrderDetailInfo()
                            {
                                OrderID = id,
                                AdminID = adminid,
                                UserID = userid,
                                ProductID = car.ProductID,
                                Name = car.Name,
                                OPrice = car.OPrice,
                                APrice = car.APrice,
                                PPrice = car.PPrice,
                                Unit = car.Unit,
                                Img = car.Img.Replace(Commons.domain, ""),
                                Count = car.Count,
                                Format = car.Format,
                                CreateDate = DateTime.Now
                            };
                            POrderDetailDAL.Save(detail);
                        }
                        CarDAL.DeleteListByPay(adminid, userid);
                    }
                    else
                    {
                        ProductInfo product = ProductDAL.FindOneByID(productid);
                        POrderDetailInfo detail = new POrderDetailInfo()
                        {
                            OrderID = id,
                            AdminID = adminid,
                            UserID = userid,
                            ProductID = productid,
                            Name = product.Name,
                            OPrice = product.OPrice,
                            APrice = product.APrice,
                            PPrice = product.PPrice,
                            Unit = product.Unit,
                            Img = product.Img.Replace(Commons.domain, ""),
                            Count = count,
                            Format = format,
                            CreateDate = DateTime.Now
                        };
                        POrderDetailDAL.Save(detail);
                    }
                }
                AdminInfo admin = AdminDAL.FindOneByID(adminid);
                WxPayConfig.APPID = admin.AppId;
                WxPayConfig.MCHID = admin.MchId;
                WxPayConfig.KEY = admin.AppKey;
                WxPayConfig.APPSECRET = admin.AppSecret;
                dynamic dy = new ExpandoObject();
                WxPayData data = new WxPayData();
                string nonce_str = WxPayApi.GenerateNonceStr();
                string timestamp = WxPayApi.GenerateTimeStamp();
                data.SetValue("body", "商城");
                data.SetValue("out_trade_no", WxPayApi.GenerateOutTradeNo());
                data.SetValue("total_fee", (int)(allmoney * 100));
                data.SetValue("openid", openid);
                data.SetValue("trade_type", "JSAPI");
                data.SetValue("attach", id + "_" + userid + "_" + adminid);
                data.SetValue("nonce_str", nonce_str);//随机字符串
                WxPayData result = WxPayApi.UnifiedOrder(data);
                dy.prepay_id = result.GetValue("prepay_id");
                dy.appid = WxPayConfig.APPID;
                dy.mchid = WxPayConfig.MCHID;
                dy.key = WxPayConfig.KEY;
                dy.appsecret = WxPayConfig.APPSECRET;
                WxPayData wp = new WxPayData();
                wp.SetValue("appId", WxPayConfig.APPID);//公众账号ID
                wp.SetValue("nonceStr", nonce_str);//随机字符串
                wp.SetValue("timeStamp", timestamp);
                wp.SetValue("signType", "MD5");
                wp.SetValue("package", "prepay_id=" + dy.prepay_id);
                dy.sign = wp.MakeSign();
                dy.package = "prepay_id=" + dy.prepay_id;
                dy.timestamp = timestamp;
                dy.noncestr = nonce_str;
                dy.ordernum = info.Num;
                return Commons.GetSuccess("保存成功", dy);
            });
            #endregion
            #region 获取微信支付参数
            hs.Add("GetWxParam2", (int adminid, int userid) =>
            {
                //参数
                int id = JobRequest.GetRequestInt("id"); //订单ID
                string openid = JobRequest.GetRequestString("openid"); //开放ID
                double allmoney = JobRequest.GetRequestDouble("allmoney"); //实付金额
                //参数
                if (string.IsNullOrEmpty(openid) || openid == "undefined")
                {
                    openid = UserDAL.FindOneByID(userid).OpenID;
                }
                AdminInfo admin = AdminDAL.FindOneByID(adminid);
                WxPayConfig.APPID = admin.AppId;
                WxPayConfig.MCHID = admin.MchId;
                WxPayConfig.KEY = admin.AppKey;
                WxPayConfig.APPSECRET = admin.AppSecret;
                dynamic dy = new ExpandoObject();
                WxPayData data = new WxPayData();
                string nonce_str = WxPayApi.GenerateNonceStr();
                string timestamp = WxPayApi.GenerateTimeStamp();
                data.SetValue("body", "商城");
                data.SetValue("out_trade_no", WxPayApi.GenerateOutTradeNo());
                data.SetValue("total_fee", (int)(allmoney * 100));
                data.SetValue("openid", openid);
                data.SetValue("trade_type", "JSAPI");
                data.SetValue("attach", id + "_" + userid + "_" + adminid);
                data.SetValue("nonce_str", nonce_str);//随机字符串
                WxPayData result = WxPayApi.UnifiedOrder(data);
                dy.prepay_id = result.GetValue("prepay_id");
                dy.appid = WxPayConfig.APPID;
                dy.mchid = WxPayConfig.MCHID;
                dy.key = WxPayConfig.KEY;
                dy.appsecret = WxPayConfig.APPSECRET;
                WxPayData wp = new WxPayData();
                wp.SetValue("appId", WxPayConfig.APPID);//公众账号ID
                wp.SetValue("nonceStr", nonce_str);//随机字符串
                wp.SetValue("timeStamp", timestamp);
                wp.SetValue("signType", "MD5");
                wp.SetValue("package", "prepay_id=" + dy.prepay_id);
                dy.sign = wp.MakeSign();
                dy.package = "prepay_id=" + dy.prepay_id;
                dy.timestamp = timestamp;
                dy.noncestr = nonce_str;
                return Commons.GetSuccess("保存成功", dy);
            });
            #endregion
        }

        //PC用
        public static void UploadAnnex(int sid, int userid, string tablename, string imgs)
        {
            string[] imges = imgs.Split(',');
            if (imgs.Trim() != "")
            {
                foreach (string img in imges)
                {
                    string[] images = img.Split('*');
                    FileInfo file = new FileInfo(HttpContext.Current.Server.MapPath(images[0]));
                    AnnexInfo info = new AnnexInfo()
                    {
                        SID = sid,
                        UserID = userid,
                        Name = images[1],
                        Path = images[0],
                        Size = (int)file.Length,
                        TableName = tablename,
                        Type = 1,
                        CreateDate = DateTime.Now
                    };
                    AnnexDAL.Save(info);
                }
            }
        }
        public static void UploadFile(int sid, int userid, string tablename, string imgs)
        {
            string[] imges = imgs.Split(',');
            if (imgs.Trim() != "")
            {
                foreach (string img in imges)
                {
                    string[] images = img.Split('*');
                    FileInfo file = new FileInfo(HttpContext.Current.Server.MapPath(images[0]));
                    AnnexInfo info = new AnnexInfo()
                    {
                        SID = sid,
                        UserID = userid,
                        Name = images[1],
                        Path = images[0],
                        Size = (int)file.Length,
                        TableName = tablename,
                        Type = 2,
                        CreateDate = DateTime.Now
                    };
                    AnnexDAL.Save(info);
                }
            }
        }
        //手机用
        public static void UploadAnnex(int sid,int userid, string tablename)
        {
            var content = HttpContext.Current;
            string opath = "/upload/" + DateTime.Now.ToString("yyyy-MM-dd") + "/";
            var rootpath = content.Server.MapPath(opath);
            if (!System.IO.Directory.Exists(rootpath))
            {
                Directory.CreateDirectory(rootpath);
            }
            string[] keys = content.Request.Files.AllKeys;
            for (int j = 0; j < content.Request.Files.Count; j++)
            {
                HttpPostedFile uploadFile = content.Request.Files[j];
                if (uploadFile.ContentLength > 0 && keys[j]=="img")
                {
                    string ext = Path.GetExtension(uploadFile.FileName);
                    string unique = DateTime.Now.ToString("HHmmssfff");
                    string file = rootpath + unique + ext;
                    string name = file.Substring(0, file.LastIndexOf('.'));
                    uploadFile.SaveAs(file);
                    string imgpath50 = name + "_50" + ext;
                    string imgpath100 = name + "_100" + ext;
                    string imgpath200 = name + "_200" + ext;
                    string imgpath500 = name + "_500" + ext;
                    string imgpath1000 = name + "_1000" + ext;
                    Thumbnail.MakeThumbnailImage(file, imgpath50, 50, 50);
                    Thumbnail.MakeThumbnailImage(file, imgpath100, 100, 100);
                    Thumbnail.MakeThumbnail(file, imgpath200, 200, 200);
                    Thumbnail.MakeThumbnailImage(file, imgpath500, 500, 500);
                    Thumbnail.MakeThumbnailImage(file, imgpath1000, 1000, 1000);

                    AnnexInfo info = new AnnexInfo()
                    {
                        SID=sid,
                        UserID=userid,
                        Name=uploadFile.FileName,
                        Path = opath + unique + ext,
                        Size=uploadFile.ContentLength,
                        TableName = tablename,
                        Type=1,
                        CreateDate=DateTime.Now
                    };
                    AnnexDAL.Save(info);
                }

                if (uploadFile.ContentLength > 0 && keys[j] == "file")
                {
                    string ext = Path.GetExtension(uploadFile.FileName);
                    string unique = DateTime.Now.ToString("HHmmssfff");
                    string file = rootpath + unique + ext;
                    if (ext == "") { continue; }
                    string name = file.Substring(0, file.LastIndexOf('.'));
                    uploadFile.SaveAs(file);
                    AnnexInfo info = new AnnexInfo()
                    {
                        SID = sid,
                        UserID = userid,
                        Name = uploadFile.FileName,
                        Path = opath + unique + ext,
                        Size = uploadFile.ContentLength,
                        TableName = tablename,
                        Type=2,
                        CreateDate = DateTime.Now
                    };
                    AnnexDAL.Save(info);

                }

            }
        }
        public static string UploadImg()
        {
            var content = HttpContext.Current;
            string opath = "/upload/" + DateTime.Now.ToString("yyyy-MM-dd") + "/";
            var rootpath = content.Server.MapPath(opath);
            if (!System.IO.Directory.Exists(rootpath))
            {
                Directory.CreateDirectory(rootpath);
            }
            string str = "";
            for (int j = 0; j < content.Request.Files.Count; j++)
            {
                HttpPostedFile uploadFile = content.Request.Files[j];
                if (uploadFile.ContentLength > 0)
                {
                    string ext = Path.GetExtension(uploadFile.FileName);
                    string unique = DateTime.Now.ToString("HHmmssfff");
                    string file = rootpath + unique + ext;
                    string name = file.Substring(0, file.LastIndexOf('.'));
                    uploadFile.SaveAs(file);
                    string imgpath50 = name + "_50" + ext;
                    string imgpath100 = name + "_100" + ext;
                    string imgpath200 = name + "_200" + ext;
                    string imgpath500 = name + "_500" + ext;
                    string imgpath1000 = name + "_1000" + ext;
                    Thumbnail.MakeThumbnailImage(file, imgpath50, 50, 50);
                    Thumbnail.MakeThumbnailImage(file, imgpath100, 100, 100);
                    Thumbnail.MakeThumbnail(file, imgpath200, 200, 200);
                    Thumbnail.MakeThumbnailImage(file, imgpath500, 500, 500);
                    Thumbnail.MakeThumbnailImage(file, imgpath1000, 1000, 1000);
                    str = opath + unique + ext;
                }

            }
            return str;
        }
        static string SendSms(string PhoneNumber, string SMSContent)
        {
            string tmp = "";
            if (PhoneNumber != "")
            {
                string accountname = "tianze";
                string accountpwd = "tianze8899";
                Encoding encoding = Encoding.GetEncoding("gb2312");
                string postData = "";
                SMSContent = System.Web.HttpUtility.UrlEncode(SMSContent, System.Text.Encoding.GetEncoding("gb2312"));
                byte[] data = encoding.GetBytes(postData);
                // Prepare web request
                HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create("http://203.81.21.34//send/gsend.asp?name=" + accountname + "&pwd=" + accountpwd + "&dst=" + PhoneNumber + "&msg=" + SMSContent);
                myRequest.Method = "POST";
                myRequest.Timeout = 10000;
                myRequest.ContentType = "application/x-www-form-urlencoded";
                myRequest.ContentLength = data.Length;
                Stream newStream = myRequest.GetRequestStream();
                // Send the data.
                newStream.Write(data, 0, data.Length);
                newStream.Close();
                //接收返回信息：
                HttpWebResponse response = (HttpWebResponse)myRequest.GetResponse();
                StreamReader sreader = new StreamReader(response.GetResponseStream(), Encoding.Default);
                tmp = sreader.ReadToEnd();
            }
            return tmp;
        }
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}